package com.yimi.yinhepda.db.dao;

import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.StorageDetailTable;
import com.yimi.yinhepda.entity.response.StorageDetail;
import com.pda.uce.commom.util.LogUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/12
 * 库区详细基础资料数据操作
 */
public class StorageDetailTableDao extends BaseDao<StorageDetailTable, Long> {

    private static final String TAG = SortScanTableDao.class.getSimpleName();

    public StorageDetailTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), StorageDetailTable.class);
    }

    public String findOrderByLatestTime() {
        String lastTime = null;
        try {
            QueryBuilder<StorageDetailTable, Long> queryBuilder = dao.queryBuilder().orderBy("latestTime", false).limit(1L);
            List<StorageDetailTable> list = queryBuilder.query();
            if (list != null && list.size() > 0) {
                lastTime = list.get(0).getLatestTime();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lastTime;
    }

    /**
     * 只查询latestTime字段的最新时间，可提高查询速度
     */
    public String findOrderByLatestTime2() {
        String lastTime = null;
        try {
            QueryBuilder<StorageDetailTable, Long> queryBuilder = dao.queryBuilder();
            queryBuilder.selectColumns("latestTime");
            queryBuilder.orderBy("latestTime", false).limit(1L);
            List<StorageDetailTable> list = queryBuilder.query();
            if (list != null && list.size() > 0) {
                lastTime = list.get(0).getLatestTime();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lastTime;
    }

    public void createOrUpdate(List<StorageDetail> list) {
        SQLiteDatabase database = getDbHelper().getWritableDatabase();
        String tableName = dao.getTableName();
        StringBuffer sql = new StringBuffer("replace into ");
        sql.append(tableName);
        sql.append("(storageDetailId,");
        sql.append("storageCode,");
        sql.append("nextCode,");
        sql.append("creater,");
        sql.append("createrTime,");
        sql.append("modifier,");
        sql.append("modifierTime,");
        sql.append("isDelete,");
        sql.append("latestTime,");
        sql.append("recordVersion,");
        sql.append("column1,");
        sql.append("column2,");
        sql.append("column3,");
        sql.append("column4,");
        sql.append("column5)");
        //15
        sql.append("values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        LogUtil.e("sql=" + sql.toString());

        database.beginTransaction();
        for (StorageDetail item : list) {
            Object[] values = new Object[]{
                    item.getStorage_detail_id(), item.getStorage_code(), item.getNext_code(), item.getCreater(), item.getCreater_time(), item.getModifier(),
                    item.getModifier_time(), item.getIs_delete(), item.getLatest_time(), item.getRecord_version(), item.getColumn1(), item.getColumn2(),
                    item.getColumn3(), item.getColumn4(), item.getColumn5()
            };
            database.execSQL(sql.toString(), values);
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    }

    /**
     * 根据库区编码查询
     */
    public StorageDetailTable findByStorageCode(String storageCode) {
        try {
            QueryBuilder<StorageDetailTable, Long> queryBuilder = dao.queryBuilder().limit(1L);
            Where where = queryBuilder.where();
            where.eq("storageCode", storageCode);
            List<StorageDetailTable> list = queryBuilder.query();
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据下一站查询
     */
    public List<StorageDetailTable> findByNextCode(String nextCode) {
        try {
            QueryBuilder<StorageDetailTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("nextCode", nextCode);
            where.and().eq("isDelete", 0);
            List<StorageDetailTable> list = queryBuilder.query();
            if (list != null && list.size() > 0) {
                return list;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
